/**
 * 根据素材名称下载素材到本地
 */

import * as path from "path";
import * as fs from "fs";

import config from "./config";
import { downPlanByName } from "./util";

(async () => {
	let planNames: string[] = config.PLAN_NAMES;
	const commandLineArgs = process.argv.slice(2);

	if (commandLineArgs.length) {
		planNames = commandLineArgs;
	}

	if (planNames.length === 0) {
		console.log("请指定需要下载的排期的名称");
		process.exit(1);
	}

	if (!fs.existsSync(config.MATERIALS_DIR)) fs.mkdirSync(config.MATERIALS_DIR);


	for (let i = 0; i < planNames.length; i++) {
		const mapInfo: any = {}
		const name = planNames[i];
		if (!mapInfo[name]) {
			mapInfo[name] = {};
		}

		const targetDir = path.resolve(config.MATERIALS_DIR, name);
		if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir);

		await downPlanByName(name.toString(), mapInfo[name], targetDir);
		await fs.promises.writeFile(`${targetDir}/mapInfo.json`, JSON.stringify(mapInfo, null, 2));
	}

	console.log("success");
})();
